package com.liwenfeng.diancanspringboot.controller.admin;

import com.liwenfeng.diancanspringboot.common.result.Result;
import com.liwenfeng.diancanspringboot.common.utils.CosUtil;
import com.liwenfeng.diancanspringboot.mapper.DishMapper;
import com.liwenfeng.diancanspringboot.pojo.entity.Dish;
import com.liwenfeng.diancanspringboot.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/admin/common")
@Slf4j
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    private CosUtil cosUtil;

    @Autowired
    private DishService dishService;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {
        String url = cosUtil.upload(file);
        if (url != null){
            return Result.success(url);
        }
        return Result.error("上传失败");
    }

    @DeleteMapping("/delete")
    @ApiOperation("文件删除")
    public Result<String> delete(@RequestBody List<String> urls) {
        log.info("删除文件：{}", urls);
        cosUtil.delete(urls);
        return Result.success("删除成功");
    }

    // 查询对象列表
    @GetMapping("/imageList")
    @ApiOperation("查询cos对象列表")
    public Result<List<String>> imageList() {
        List<String> list = cosUtil.list();
        return Result.success(list);
    }

    @PostMapping("/clearUseless")
    @ApiOperation("清理无用图片")
    public Result<String> clearUseless() {
        log.info("清理无用图片");
        // 有图片的表：dish,order_detail,setmeal,shopping_cart,user
        // todo
//        List<Dish> list1 = dishService.list();
//        List<String> urls = new ArrayList<>(list1.stream().map(Dish::getImage).toList());
        List<String> urls =List.of("https://popop-1333907626.cos.ap-beijing.myqcloud.com/diancan/1735630177520-217.png");
//        cosUtil.clearUseless(urls);
        return Result.success("清理成功");
    }

}
